package com.lzw.tool.ffmpeg;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.kokorin.jaffree.LogLevel;
import com.github.kokorin.jaffree.StreamType;
import com.github.kokorin.jaffree.ffmpeg.FFmpeg;
import com.github.kokorin.jaffree.ffmpeg.FFmpegResult;
import com.github.kokorin.jaffree.ffmpeg.UrlInput;
import com.github.kokorin.jaffree.ffmpeg.UrlOutput;
import com.github.kokorin.jaffree.ffprobe.FFprobe;
import com.github.kokorin.jaffree.ffprobe.FFprobeResult;
import com.github.kokorin.jaffree.ffprobe.Stream;
import com.github.kokorin.jaffree.ffprobe.data.DSection;
import com.github.kokorin.jaffree.ffprobe.data.Data;

import java.io.File;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utilffprobe {

    private static final ObjectMapper objectMapper = new ObjectMapper();
    public static void main(String[] args) {
        final Map<String, Object> mediaInfo;
        try {
//            final String mediaData = getMediaData();
            System.out.println(System.currentTimeMillis());
            mediaInfo = getMediaInfo();
//            System.out.println(mediaInfo);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }
    public static Map<String, Object> getMediaInfo() throws JsonProcessingException {
//        String mediaPath = "https://liveplay-testplayback.jravity.com/8d984842vodcq1300494236/de47fbf6387702297253670283/Zal0DbaP1moA.mp4";
        
        String mediaPath = "D:\\迅雷下载\\1.mp4";
        FFprobe fFprobe = new FFprobe(Paths.get("D:\\tools\\ffmpeg-win64-gpl\\bin\\ffprobe.exe"));

        FFprobeResult result = fFprobe
                .setShowStreams(true)
                .setInput(mediaPath)
                .execute();

        List<Stream> streamList = result.getStreams();

        Map<String, Object> mediaInfoMap = new HashMap<>(3);

        //只取视频的宽高即可
        for (Stream stream : streamList) {
            if (StreamType.VIDEO.equals(stream.getCodecType())) {
                mediaInfoMap.put("codecType", stream.getCodecType());
                mediaInfoMap.put("height", stream.getHeight());
                mediaInfoMap.put("width", stream.getWidth());
            }
//            String s = objectMapper.writeValueAsString(stream);
//            System.out.println(s);
            System.out.println(stream.getCodecType()+":"+stream.getDurationTs());
        }
        return mediaInfoMap;
    }

    public static String getMediaData() throws JsonProcessingException {
        String mediaPath = "https://liveplay-testplayback.jravity.com/8d984842vodcq1300494236/de47fbf6387702297253670283/Zal0DbaP1moA.mp4";

//        String mediaPath = "D:\\tools\\ffmpeg-win64-gpl\\bin\\123eo.mp4";
        String out = "D:\\tools\\ffmpeg-win64-gpl\\bin\\123eo.jpeg";
        FFmpeg fFmpeg = new FFmpeg(Paths.get("D:\\tools\\ffmpeg-win64-gpl\\bin\\ffprobe.exe"));

        final File file = new File("D:\\tools\\ffmpeg-win64-gpl\\out.123.mp4");

        final FFmpegResult execute = fFmpeg.setLogLevel(LogLevel.ERROR)
                .addInput(UrlInput.fromPath(Paths.get(mediaPath)))
//                .addArguments("-ss", "1")
//                .addArguments("-i", mediaPath)
//                .addArguments("-vframes", "1")
                .addOutput(UrlOutput.toPath(Paths.get(out)))
                .execute();
        return "";
//./ffmpeg.exe -ss 00:00:40  -i .\123eo.mp4 -vframes 1 image1-%05d.jpeg
    }
}
